/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import xmlobject.FlowerList.FlowerAtt;
import xmlobject.FlowerList.FlowerList;
/**
 *
 * @author Antv60287
 */
public class FlowerListDAO {


 //   public static void main(String[] args) throws SQLException, ClassNotFoundException, JAXBException {
             public static void FlowerListtoXML(String path,int categoryId) throws ClassNotFoundException, SQLException, JAXBException {
        /**
         * lay du lieu tu db
         */
        FlowerList flowerList = new FlowerList();

        Config.Connection config = new Config.Connection();
        Connection con = config.getConnection();
        PreparedStatement sm = null;
        ResultSet result = null;
        try {
            String sql = "Select * from tblProduct where CategoryID = ?";
            sm = con.prepareStatement(sql);
            sm.setInt(1, categoryId);
            result = sm.executeQuery();

            /**
             *  dua du lieu vao list
             */
            while (result.next()) {
                int productId = result.getInt("ProductId");
                String productName = result.getString("ProductName");
                int price = result.getInt("Price");
                int productUnit = result.getInt("ProductUnit");
                String image = result.getString("Image");

                FlowerAtt flowerAtt = new FlowerAtt();
                flowerAtt.setImage(image);
                flowerAtt.setPrice(price);
                flowerAtt.setProductId(productId);
                flowerAtt.setProductName(productName);
                flowerAtt.setProductUnit(productUnit);
                flowerList.getFlower().add(flowerAtt);
                }
        } catch (Exception e) {
            System.out.println("open fail: " + e);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    System.out.println("close fail: " + e);
                }
            }
        }


        /**
         *  marshall ra xml
         */
        try {

            JAXBContext context = JAXBContext.newInstance(FlowerList.class);
            Marshaller mar = context.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            File list = new File(path+"XML/FlowerCategory"+categoryId +".xml");
            mar.marshal(flowerList, list);
            System.out.println("create "+path+"web/XML/FlowerCategory"+categoryId +".xml"+" successfully!");

        } catch (JAXBException e) {
            System.out.println("create "+path+"web/XML/FlowerCategory"+categoryId +".xml"+" fail: " + e);
        }

    }
}
